package com.soar.recruitment.module.home.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;

import com.soar.recruitment.R;
import com.soar.recruitment.common.ui.fragment.SOARBaseBackFragment;
import com.soar.recruitment.common.ui.view.ptr.PtrFrameInit;
import com.soar.recruitment.common.ui.view.ptr.RecyclerViewInit;
import com.soar.recruitment.module.home.dal.bean.RecommendModle;
import com.soar.recruitment.module.home.ui.adapter.RecommendAdapter;
import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by chriswen on 2017/7/1.
 */

public class RecommendDetailFragment extends SOARBaseBackFragment {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.container_ptr_frame)
    PtrFrameLayout containerPtrFrame;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private View mHeaderView;
    private View mEmptyView;

    private RecommendAdapter mRecommendAdapterr;
    private List<RecommendModle> mRecommendModleList;

    public static RecommendDetailFragment newInstance() {
        Bundle args = new Bundle();
        RecommendDetailFragment fragment = new RecommendDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void initVariable() {

    }

    @Override
    public void initView(Bundle savedInstanceState) {
        toolbar.setTitle("招考推荐");
        initToolbarNav(toolbar);

        initRecycleView();

        LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
        mHeaderView = layoutInflater.inflate(R.layout.header_exam, recyclerView, false);
        mRecommendAdapterr = new RecommendAdapter(_mActivity, R.layout.item_home, mRecommendModleList);
        HeaderAndFooterWrapper headerAndFooterWrapper = new HeaderAndFooterWrapper(mRecommendAdapterr);
        headerAndFooterWrapper.addHeaderView(mHeaderView);
        recyclerView.setAdapter(headerAndFooterWrapper);
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecycleView() {
        PtrFrameInit.init(containerPtrFrame, mActivity, false);
        containerPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, recyclerView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {

            }
        });
        containerPtrFrame.disableWhenHorizontalMove(true);
        containerPtrFrame.autoRefresh(true);

        RecyclerViewInit.init(recyclerView, mActivity);
    }

    @Override
    public void initData() {
        mRecommendModleList = new ArrayList<>();
        RecommendModle recommendModle = null;
        for (int i = 0; i < 5; i++) {
            recommendModle = new RecommendModle();
            mRecommendModleList.add(recommendModle);
        }
        mRecommendAdapterr.setData(mRecommendModleList);
    }
}